<?php
//支付记录模型
class PayLogModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'pay_log';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = "  ";		
		
		$sql  = "select * from ".$this->db->dbprefix.$this->table." {$where} LIMIT ".($page-1)*$size .",{$size}";
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table.$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists(){
		$where = "  ";
		$sql  = "select * from ".$this->db->dbprefix.$this->table.$where;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table."  where id='{$id}'  ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
	function add_log($user_id,$amount,$cinfo=array()){
		
		$time = time();
		$order_sn = '';
		
		while(true){
			$order_sn = $time.date('Ymd',$time).'_'.rand(1000,9999);
			$is_exist = $this->_where_exist(array('order_sn'=>$order_sn));
			if(!$is_exist){
				break;
			}
		}
		
		$arr = array(
			'user_id'=>$user_id,
			'order_sn'=>$order_sn,
			'amount'=>$amount,
			'addtime'=>$time,
			'card_id'=>$cinfo['id'],
			'form_id'=>$cinfo['form_id'],
			'goods_name'=>$cinfo['card_name']
		);
		$result = $this->add($arr);
		if($result){
			return $arr;
		}
		return false;
	}
	
	function order_get_info($order_sn){
		$sql  = "select * from ".$this->db->dbprefix.$this->table."  where order_sn='{$order_sn}' AND `status`=0 limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

